/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.z100h.supplychain.core.biz.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.sys.entity.User;
import com.jeeplus.modules.sys.utils.UserUtils;
import com.z100h.supplychain.core.EvidenceLockState;
import com.z100h.supplychain.core.ResourceStockoutLockState;
import com.z100h.supplychain.core.biz.entity.Resource;
import com.z100h.supplychain.core.biz.entity.ResourceDetail;
import com.z100h.supplychain.core.biz.mapper.ResourceMapper;
import com.z100h.supplychain.warehouse.entity.Warehouse.WarehouseType;

/**
 * 产品资源Service
 *
 * @author madara
 * @version 2019-01-04
 */
@Service
@Transactional(readOnly = true)
public class ResourceService extends CrudService<ResourceMapper, Resource> {

	@Autowired
	private ResourceMapper resourceMapper;

	/**
	 * 插入资源
	 * @param resource
	 */
	@Transactional(readOnly = false)
	public void addResource(Resource resource) {
		resource.preInsert();
		resourceMapper.addResource(resource);
	}

	/**
	 * 批量插入资源
	 * @param resource
	 */
	@Transactional(readOnly = false)
	public void addBatchResource(Resource resource) {
		// TODO
		resourceMapper.addBatchResource(resource);
	}

	/**
	 * 
	 * 校验资源
	 * 
	 * @param evidenceId
	 * @param resourceLockState
	 * @param resourceStockoutLockState
	 * @param resourceIds
	 * @param warehouseType
	 * @return
	 * @author: Fyh - 1009173473@qq.com 
	 * @date: Created on 2019年1月19日 下午3:50:24
	 */
	@Transactional(readOnly = false)
	public List<String> checkResource(String evidenceId, EvidenceLockState evidenceLockState,
			ResourceStockoutLockState resourceStockoutLockState, List<String> resourceIds,
			WarehouseType warehouseType) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("evidenceId", evidenceId);
		param.put("resourceLockState", evidenceLockState.getValue());
		param.put("stockoutLockState", resourceStockoutLockState.getValue());
		param.put("resourceIds", resourceIds);

		if (null != warehouseType && null != warehouseType.getValue()) {
			param.put("warehouseType", warehouseType.getValue());// TODO 这里需要做非空判断
		}
		return resourceMapper.checkResource(param);
	}

	/**
	 * 凭证操作 锁定资源
	 * @param resourceIds
	 * @param resourceLockState
	 */
	@Transactional(readOnly = false)
	public void lockResource(List<String> resourceIds, EvidenceLockState resourceLockState) {
		User user = UserUtils.getUser();
		// TODO mapper
		// TODO
		Map<String, Object> param = new HashMap<String, Object>();
		for (String resourceId : resourceIds) {
			param.put("code", resourceId);
			param.put("resourceLockState", resourceLockState.getValue());
			param.put("updateBy", user.getId());
			param.put("updateDate", new Date());
			resourceMapper.lockResources(param);
		}

	}

	/**
	 * 根据凭证id 锁定资源
	 * @param evidenceId
	 * @param resourceLockState
	 */
	@Transactional(readOnly = false)
	public void lockResource(String evidenceId, EvidenceLockState resourceLockState) {
		Map<String, Object> param = new HashMap<String, Object>();
		User user = UserUtils.getUser();
		param.put("evidenceId", evidenceId);
		param.put("resourceLockState", resourceLockState.getValue());
		param.put("updateBy", user.getId());
		param.put("updateDate", new Date());
		resourceMapper.lockResourceByEid(param);
	}

	/**
	 * 解锁资源
	 *
	 * @param resourceIds
	 * @param
	 */
	@Transactional(readOnly = false)
	public void unlockResource(List<String> resourceIds, EvidenceLockState resourceLockState,
			ResourceStockoutLockState resourceStockoutLockState) {
		Map<String, Object> param = new HashMap<String, Object>();
		User user = UserUtils.getUser();
		for (String resourceId : resourceIds) {
			param.put("code", resourceId);
			if (null != resourceLockState) {
				param.put("resourceLockState", resourceLockState.getValue());
			}
			if (null != resourceStockoutLockState) {
				param.put("resourceStockoutLockState", resourceStockoutLockState.getValue());
			}
			param.put("updateBy", user.getId());
			param.put("updateDate", new Date());
			resourceMapper.unlockResources(param);
		} // TODO 批量

	}

	/**
	 * 凭证id 解锁资源
	 * @param evidenceId
	 * @param resourceLockState
	 */
	@Transactional(readOnly = false)
	public void unlockResourceByEid(String evidenceId, EvidenceLockState resourceLockState,
			ResourceStockoutLockState resourceStockoutLockState) {
		Map<String, Object> param = new HashMap<String, Object>();
		User user = UserUtils.getUser();
		if (null != resourceLockState) {
			param.put("resourceLockState", resourceLockState.getValue());
		}
		if (null != resourceStockoutLockState) {
			param.put("resourceStockoutLockState", resourceStockoutLockState.getValue());
		}
		param.put("evidenceId", evidenceId);
		param.put("updateBy", user.getId());
		param.put("updateDate", new Date());
		resourceMapper.unlockResourceByEid(param);
	}

	/**
	 * 修改资源仓库id
	 *
	 * @param resourceIds
	 * @param warehouseId
	 */
	@Transactional(readOnly = false)
	public void updateResourceToWarehouseId(List<String> resourceIds, String warehouseId) {
		Map<String, Object> param = new HashMap<String, Object>();
		User user = UserUtils.getUser();
		param.put("resourceIds", resourceIds);
		param.put("warehouseId", warehouseId);
		param.put("updateBy", user.getId());
		param.put("updateDate", new Date());
		resourceMapper.updateResourcesToWarehouseId(param);
	}

	/**
	 * 修改资源凭证id
	 *
	 * @param resourceIds
	 * @param evidenceId
	 */
	@Transactional(readOnly = false)
	public void associateEvidenceWithResource(final String evidenceId, final List<String> resourceIds) {
		Map<String, Object> param = new HashMap<String, Object>();
		User user = UserUtils.getUser();
		param.put("evidenceId", evidenceId);
		param.put("resourceIds", resourceIds);
		param.put("updateBy", user.getId());
		param.put("updateDate", new Date());
		resourceMapper.updateResourcesToEvidenceId(param);
	}

	/**
	 * 
	 * 查询资源信息
	 * 
	 * @param ids
	 * @return
	 * @author: Fyh - 1009173473@qq.com 
	 * @date: Created on 2019年1月11日 上午10:47:42
	 */
	public List<ResourceDetail> findResourceDetailByIds(List<String> ids) {
		return resourceMapper.findResourceDetailByIds(ids);
	}

	/**
	 * 
	 * 查询资源列表
	 * 
	 * @param page
	 * @param entity
	 * @return
	 * @author: Fyh - 1009173473@qq.com 
	 * @date: Created on 2019年1月14日 下午7:17:32
	 */
	public Page<ResourceDetail> findResourceDetailList(Page<ResourceDetail> page, ResourceDetail entity) {
		dataRuleFilter(entity);
		entity.setPage(page);
		page.setList(resourceMapper.findResourceDetailList(entity));
		return page;
	}

	/**
	 *
	 * 检查出库资源id
	 *
	 * @param ids
	 * @return
	 * @author: madara
	 * @date: Created on 2019年1月11日 上午10:47:42
	 */
	public List<ResourceDetail> checkResourceIdsByStockOut(List<String> ids) {
		return resourceMapper.findResourceDetailByIds(ids);
	}

	/**
	 * 
	 * 修改出库锁状态
	 * 
	 * @param ids
	 * @param state
	 * @author: Fyh - 1009173473@qq.com 
	 * @date: Created on 2019年1月19日 上午11:57:22
	 */
	public void updateStockoutState(@Param("ids") List<String> ids, @Param("state") Integer state) {
		resourceMapper.updateStockoutState(ids, state);
	}

	/**
	 * 
	 * 根据eid查询资源编码集合
	 * 
	 * @param evidenceId
	 * @return
	 * @author: Fyh - 1009173473@qq.com 
	 * @date: Created on 2019年1月19日 下午5:27:28
	 */
	public List<String> findIdByEid(String evidenceId) {
		return resourceMapper.findIdByEid(evidenceId);
	}

	/**
	 * 
	 * 查询资源sku数量
	 * 
	 * @param ids
	 * @return
	 * @author: Fyh - 1009173473@qq.com 
	 * @date: Created on 2019年1月30日 下午3:55:12
	 */
	public List<ResourceDetail> findResourceCountByIds(List<String> ids) {
		return resourceMapper.findResourceCountByIds(ids);
	}

	/**
	 * 
	 * 查询可用的sku资源数量
	 * 
	 * @param skuId
	 * @return
	 * @author: Fyh - 1009173473@qq.com 
	 * @date: Created on 2019年5月15日 上午10:07:58
	 */
	public Integer getUsableResourceBySkuId(Integer skuId, String companyId) {
		return resourceMapper.getUsableResourceBySkuId(skuId, companyId);
	}

	/**
	 * 
	 * 根据skuId出库
	 * 
	 * @param skuId
	 * @param lockState
	 * @param evidenceId
	 * @param amount
	 * @author: Fyh - 1009173473@qq.com 
	 * @date: Created on 2019年5月15日 上午10:41:10
	 */
	@Transactional(readOnly = false)
	public void updateResourceStockout(Integer skuId, Integer lockState, String evidenceId, Integer amount,
			String companyId) {
		resourceMapper.updateResourceStockout(skuId, lockState, evidenceId, amount, companyId);
	}

	/**
	 * 
	 * 根据skuId出库
	 * 
	 * @param skuId
	 * @param lockState
	 * @param evidenceId
	 * @param amount
	 * @author: Fyh - 1009173473@qq.com 
	 * @date: Created on 2019年5月15日 上午10:41:10
	 */
	@Transactional(readOnly = false)
	public void updateResourceStockoutToErp(String erpResId, Integer skuId, Integer lockState, String evidenceId,
			Integer amount, String companyId) {
		resourceMapper.updateResourceStockoutToErp(erpResId, skuId, lockState, evidenceId, amount, companyId);
	}
}